<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>sessionStorage</title>
</head>
<body>
    <h2>sessionStorage</h2>
    <button onclick="saveData()">点击保存数据</button>
    <button onclick="readData()">获取数据</button>
    <button onclick="delData()">删除数据</button>
    <button onclick="clearData()">清空数据</button>
</body>
<script type="text/javascript">
    function saveData(){
       sessionStorage.setItem("msg1","msg1");
       sessionStorage.setItem("msg2",666);

       let p = {name:'JJ',age:18}
       sessionStorage.setItem("person",JSON.stringify(p));

    }

    function readData(){
        let msg1 =  sessionStorage.getItem("msg1");
        let msg2 =  sessionStorage.getItem("msg2");
        let msg3 =  sessionStorage.getItem("person");
      
        console.log(msg1)
        console.log(msg2)
        console.log(JSON.parse(msg3))

        //key不存在 获取为null
        console.log(JSON.parse( sessionStorage.getItem("XX")))
    }

    function delData(){
        sessionStorage.removeItem("msg1")
    }

    function clearData(){
        sessionStorage.clear()
    }
</script>
</html>